package me.monster.smarttraffic.ui.car;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatSpinner;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import me.monster.smarttraffic.R;
import me.monster.smarttraffic.base.AbstractPresenterActivity;
import me.monster.smarttraffic.tool.MsgClickListener;
import me.monster.smarttraffic.tool.MsgDialog;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-12 10:33
 */
public class CarActivity extends AbstractPresenterActivity<CarPresenter> implements CarContract.View {

    @BindView(R.id.sp_car_query)
    AppCompatSpinner spQuery;
    @BindView(R.id.btn_car_query)
    Button btnQuery;
    @BindView(R.id.sp_car_charge_query)
    AppCompatSpinner spCharge;
    @BindView(R.id.et_car_charge)
    EditText etCharge;
    @BindView(R.id.btn_car_charge)
    Button btnCharge;
    @BindView(R.id.pb_char)
    ProgressBar pbChar;

    public static void start(Activity context) {
        Intent starter = new Intent(context, CarActivity.class);
        context.startActivity(starter);
        context.overridePendingTransition(R.anim.scale_in, R.anim.alpha_out);
    }

    @Override
    public CarPresenter createPresenter() {
        return new CarPresenter();
    }

    @Override
    public View closeToolView() {
        return findViewById(R.id.tv_car_tittle_account);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter.listCar();
    }

    @Override
    public String getInitTitle() {
        return getString(R.string.dash_menu_c);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_car;
    }

    @Override
    public void showLoading() {
        pbChar.setVisibility(View.VISIBLE);
    }

    @Override
    public void dismissLoading() {
        pbChar.setVisibility(View.GONE);
    }

    @OnClick(R.id.btn_car_query)
    public void queryCarInfo() {
        mPresenter.query(spCharge.getSelectedItemPosition());
    }

    @OnClick(R.id.btn_car_charge)
    public void charCar() {
        mPresenter.charge(spCharge.getSelectedItemPosition(), etCharge.getText().toString().trim());
    }

    @Override
    public void bindCars(List<Map<String, String>> carNos) {
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, carNos, android.R.layout.simple_list_item_1, new String[]{"no"}, new int[]{android.R.id.text1});
        spQuery.setAdapter(simpleAdapter);
        spCharge.setAdapter(simpleAdapter);
    }

    @Override
    public void showError(int error) {
        switch (error) {
            case CarPresenter.ERROR_CAR_LIST:
                toast("获取车辆列表出错");
                break;
            case CarPresenter.ERROR_NO_INPUT:
                toast("请输入金额");
                break;
            case CarPresenter.ERROR_NO_SELECT:
                toast("请选择车辆");
                break;
            default:
        }
    }

    @Override
    public void showCarInfo(Map<String, String> queryInfo) {
        MsgDialog.Companion.getInstance(this)
                .setTittle("查询结果")
                .setMessage(getString(R.string.car_query_result, queryInfo.get("carNo"), queryInfo.get("now")))
                .setPositive(R.string.app_positive, new MsgClickListener() {
                    @Override
                    public void onClick(int what) {

                    }
                })
                .show();

    }

    @Override
    public void chargeResult(boolean result) {
        toast(result ? "充值成功" : "充值失败");
    }
}
